﻿using SDATA;
using SGUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0002 : Form
    {
        public frm0002()
        {
            InitializeComponent();
        }

        private void frm0002_Load(object sender, EventArgs e)
        {
            LANG.TextForm(this, FRM.Lang);

            GUI.ChangeDateFormat(dtpDateAction, FRM.SystemDateFormat);
            GUI.ChangeDateFormat(dtpDateActionFrom, FRM.SystemDateFormat);
            GUI.ChangeDateFormat(dtpDateActionTo, FRM.SystemDateFormat);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void dtpDateAction_ValueChanged(object sender, EventArgs e)
        {
            dtpDateActionFrom.Value = new DateTime(dtpDateAction.Value.Year, dtpDateAction.Value.Month, 1);
            DateTime nextFirst;
            if (dtpDateAction.Value.Month == 12)
                nextFirst = new DateTime(dtpDateAction.Value.Year + 1, 1, 1);
            else nextFirst = new DateTime(dtpDateAction.Value.Year, dtpDateAction.Value.Month + 1, 1);
            dtpDateActionTo.Value = nextFirst.AddDays(-1);
        }

        private void frm0002_Shown(object sender, EventArgs e)
        {
            Load_Division();
            //Load_Period();
            dtpDateAction_ValueChanged(null, null);
            dtpDateAction.Value = FRM.DateDue;
            cbxDivision.SelectedItem = FRM.DivisionID;
            cbxMonthWork.SelectedItem = string.Format("{0}/{1}", FRM.Month, FRM.Year);
            dtpDateAction.Value = FRM.DateDue;
        }

        public void Load_Division()
        {
            // DivisionID
            if (FRM.ConnectString == "") { return; }
            cbxDivision.DataSource = DATA.GetListSimple("select DivisionID from tb0038", FRM.ConnectString);
            
        }

        public void Load_Period()
        {
            // Kỳ kế toán
            string sql = "";
            sql = "SELECT MonthYear FROM vi0317 ";
            sql += " WHERE DivisionID = '" + cbxDivision.SelectedItem.ToString() + "'";
            sql += " ORDER BY RIGHT(MonthYear,4) DESC, LEFT(MonthYear,2) DESC";
            cbxMonthWork.DataSource = DATA.GetListSimple(sql, FRM.ConnectString);
        }

        private void cbxDivision_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_Period();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FRM.DivisionID = cbxDivision.SelectedItem.ToString();
            string[] arrTemp = cbxMonthWork.SelectedItem.ToString().Split('/');
            FRM.Month = arrTemp[0];
            FRM.Year = arrTemp[1];
            FRM.DateDue = dtpDateAction.Value;
            FRM.Period = string.Format("{0}/{1}", FRM.Month, FRM.Year);
            (Application.OpenForms["frmHOME"] as frmHOME).loadGUI_Form();
            btnClose_Click(null, null);

            string sql = string.Format("UPDATE tb0000 SET DefTranMonth='{0}', DefTranYear='{1}'",FRM.Month.PadLeft(2,'0'),FRM.Year);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);

        }

        private void cbxMonthWork_SelectedIndexChanged(object sender, EventArgs e)
        {
            dtpDateAction.Value = new DateTime(int.Parse(cbxMonthWork.Text.Substring(3, 4)), int.Parse(cbxMonthWork.Text.Substring(0, 2)), 1);
        }
    }
}
